package com.c.ctgproject.adapter.guang

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.CarHandlerOverEntity
import com.c.ctgproject.ui.collect.CarHandlerDetailActivity
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.MyLog
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.noDataHandler
import org.jetbrains.anko.startActivity
import java.lang.StringBuilder

class CarHandlerOverAdapter(private val context: Context, private val mList: List<CarHandlerOverEntity.DataBean.ListBean>, private val listener: CarHandlerOverAdapterListener) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_car_handler_over_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            data.dismantleNo.noDataHandler().also { holder.mOrderNumberTV.text = it }
            holder.mCreateNameTV.text = data.createPersonName.noDataHandler()
//            holder.carPlate.text = data.carPlate.noDataHandler()
            //先别用
//            holder.carCode.text = data.carCode.noDataHandler()
            holder.mTimeTV.text = data.createTime.noDataHandler()
            val dismantleMethod = when (data.dismantleMethod) {
                1 -> "毁型"
                2 -> "拆配件"
                else -> "暂无数据"
            }
            holder.mModelTV.text = dismantleMethod

            holder.mPlanTimeTV.text = data.plainFinishTime.noDataHandler()
            holder.mFinishTimeTV.text = data.realFinishTime.noDataHandler()
            holder.mFinishTimeLL.visibility = if (data.dismantleStatus == 2) View.VISIBLE else View.GONE

            val codes = StringBuilder()
            data.carList?.let { list ->
                list.forEachIndexed { index, it ->
                    if (index == list.size - 1) {
                        val carPlate = if (it.carPlate.handlerNull().isNotEmpty()) {
                            "(${it.carPlate.handlerNull()})"
                        } else {
                            ""
                        }
                        //最后一条数据就不加逗号了
                        codes.append(it.carCode.handlerNull() + carPlate)
                    } else {
                        codes.append("${it.carCode.handlerNull()},")
                    }
                }
            }
            if (codes.isEmpty()) {
                codes.append("暂无数据")
            }
            holder.mCarCodeTV.text = codes


//            //拆解单状态 0-待拆解 1-拆解中 2-拆解完成
            when (data.dismantleStatus) {
                0 -> {
                    holder.mFinishRL.visibility = View.VISIBLE
                    holder.mLine.visibility = View.VISIBLE
                }
                1 -> {
                    holder.mFinishRL.visibility = View.VISIBLE
                    holder.mLine.visibility = View.VISIBLE
                }
                2 -> {
                    holder.mFinishRL.visibility = View.GONE
                    holder.mLine.visibility = View.GONE
                }
                else -> {
                    holder.mFinishRL.visibility = View.VISIBLE
                    holder.mLine.visibility = View.VISIBLE
                }
            }


            holder.mFinishRL.setOnClickListener {
                holder.mFinishBTN.performClick()
            }

            //dismantleMethod  1:毁型 2:拆配件
            holder.mFinishBTN.setOnClickListener {
                //拆解完成
                MyLog.d("拆解完成")
                listener.onHandlerCar(position)
            }
//            holder.mUploadImageRL.setOnClickListener {
//                holder.mUploadImageBTN.performClick()
//            }
//            holder.mUploadImageBTN.setOnClickListener {
//                MyLog.d("传照片")
//                //上传照片
//                listener.onUpLoadImage(position)
//            }
            //拆解详情
            holder.mParentLL.setOnClickListener {
                context.startActivity<CarHandlerDetailActivity>(MyConstants.ID to data.carDismantleId)
            }
        }
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
//        val carPlate: TextView = view.findViewById(R.id.carPlate)
        val mCarCodeTV: TextView = view.findViewById(R.id.mCarCodeTV)
        val mOrderNumberTV: TextView = view.findViewById(R.id.mOrderNumberTV)
//        val carCode: TextView = view.findViewById(R.id.carCode)
//        val mStatusTV: TextView = view.findViewById(R.id.mStatusTV)

        //        val mCarCodeTV: TextView = view.findViewById(R.id.mCarCodeTV)
        val mCreateNameTV: TextView = view.findViewById(R.id.mCreateNameTV)
        val mFinishTimeTV: TextView = view.findViewById(R.id.mFinishTimeTV)
        val mModelTV: TextView = view.findViewById(R.id.mModelTV)

        //        val mStatusTV: TextView = view.findViewById(R.id.mStatusTV)
        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val mPlanTimeTV: TextView = view.findViewById(R.id.mPlanTimeTV)
        val mFinishBTN: Button = view.findViewById(R.id.mFinishBTN)

        //        val mUploadImageBTN: Button = view.findViewById(R.id.mUploadImageBTN)
//        val mUploadImageRL: RelativeLayout = view.findViewById(R.id.mUploadImageRL)
        val mFinishRL: RelativeLayout = view.findViewById(R.id.mFinishRL)
        val mFinishTimeLL: LinearLayoutCompat = view.findViewById(R.id.mFinishTimeLL)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
        val mLine: View = view.findViewById(R.id.mLine)
    }

    interface CarHandlerOverAdapterListener {
        fun onHandlerCar(position: Int)
        fun onUpLoadImage(position: Int)
    }
}